package com.epalmpay.commom;




import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.epalmpay.config.Global;
import com.epalmpay.entity.Page;
import com.epalmpay.util.Logger;
import com.epalmpay.util.PageData;
import com.epalmpay.util.UuidUtil;


/**
 * @authorzsj
 * 修改时间：2017、4、11
 */
@Component
public class BaseController {
	

	public @Value("${PAY.PAYMENT_QT_URL}") 
	String PAYMENT_QT_URL;
	public @Value("${PAY.CER_FILE_PATH}") 
	String CER_FILE_PATH;
	public @Value("${PAY.PAYMENT_URL}")
	String PAYMENT_URL;
	public @Value("${PAY.HOME_URL}") 
	String HOME_URL;//项目路径 带项目名称
	public @Value("${PAY.ROOT_HOME_URL}") 
	String ROOT_HOME_URL;//项目路径不 带项目名称
	
	@Value("${EPALMPAY.SALEMAN_ROLEID}")  
	public String SALEMAN_ROLEID;
	
	@Value("${PAY.TK_URL}") 
	public String PAY_TK_URL;

	@Value("${PAY.XL_URL}") 
	public String PAY_XL_URL;

	@Value("${PAY_XDL_URL}")
	public String PAY_XDL_URL;


	
	private static ThreadLocal<HttpServletRequest> request_threadLocal = new ThreadLocal<HttpServletRequest>();
    private static ThreadLocal<HttpServletResponse> reponse_threadLocal = new ThreadLocal<HttpServletResponse>();
  
	
    @ModelAttribute  
    public void setReqAndRes(HttpServletRequest request, HttpServletResponse response) {
    	request_threadLocal.set(request);
    	reponse_threadLocal.set(response);
    	Global.init(request.getSession().getServletContext(),PAYMENT_URL,CER_FILE_PATH,PAYMENT_QT_URL,SALEMAN_ROLEID,HOME_URL,PAY_TK_URL,PAY_XL_URL,PAY_XDL_URL);
    } 
    
	protected Logger logger = Logger.getLogger(this.getClass());

	
	/** new PageData对象
	 * @return
	 */
	public PageData getPageData(){
		return new PageData(this.getRequest());
	}
	
	/**得到ModelAndView
	 * @return
	 */
	public ModelAndView getModelAndView(){
		return new ModelAndView();
	}
	
	/**得到request对象1
	 * @return
	 */
	public HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		return request;
	}

	/**得到32位的uuid
	 * @return
	 */
	public String get32UUID(){
		return UuidUtil.get32UUID();
	}
	
	/**得到分页列表的信息
	 * @return
	 */
	public Page getPage(){
		return new Page();
	}
	
	public static void logBefore(Logger logger, String interfaceName){
		logger.info("");
		logger.info("start");
		logger.info(interfaceName);
	}
	
	public static void logAfter(Logger logger){
		logger.info("end");
		logger.info("");
	}

	public void setPAYMENT_QT_URL(String pAYMENT_QT_URL) {
		PAYMENT_QT_URL = pAYMENT_QT_URL;
	}

	public void setCER_FILE_PATH(String cER_FILE_PATH) {
		CER_FILE_PATH = cER_FILE_PATH;
	}

	public void setPAYMENT_URL(String pAYMENT_URL) {
		PAYMENT_URL = pAYMENT_URL;
	}

	public void setHOME_URL(String hOME_URL) {
		HOME_URL = hOME_URL;
	}


    
	
	public void setLogger(Logger logger) {
		this.logger = logger;
	}
	
	 public Object getRequestParam(String paramName,Object defaultValue){
	    	Object temp=request_threadLocal.get().getParameter(paramName);
	    	
	    	return temp != null ? temp : defaultValue;
	    }
	    public String getRequestParamString(String paramName,String defaultValue){
	    	String temp=request_threadLocal.get().getParameter(paramName);
	    	return StringUtils. isNotBlank(temp) ? temp : defaultValue;
	    }
	    public Integer getRequestParamInteger(String paramName,Integer defaultValue){
	    	String temp=request_threadLocal.get().getParameter(paramName);
	    	try {
	    		defaultValue=(temp != null && temp.length()>0) ? Integer.valueOf(temp) : defaultValue;
	    	} catch (Exception e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	    	}   
	    	return defaultValue;
	    }
	    
	    public Long getRequestParamLong(String paramName,Long defaultValue){
	    	String temp=request_threadLocal.get().getParameter(paramName);
	    	try {
	    		defaultValue=(temp != null && temp.length()>0) ? Long.valueOf(temp) : defaultValue;
	    	} catch (Exception e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	    	}   
	    	return defaultValue;
	    }
	    public Double getRequestParamDouble(String paramName,Double defaultValue){
	    	String temp=request_threadLocal.get().getParameter(paramName);
	    	try {
	    		defaultValue=(temp != null && temp.length()>0) ? Double.valueOf(temp) : defaultValue;
	    	} catch (Exception e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	    	}  
	    	return defaultValue;
	    }
	    public Float getRequestParamFloat(String paramName,Float defaultValue){
	    	String temp=request_threadLocal.get().getParameter(paramName);
	    	try {
	    		defaultValue=(temp != null && temp.length()>0) ? Float.valueOf(temp) : defaultValue;
	    	} catch (Exception e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	    	} 
	    	return defaultValue;
	    }
	    public Byte getRequestParamByte(String paramName,Byte defaultValue){
	    	String temp=request_threadLocal.get().getParameter(paramName);
	    	try {
	    		defaultValue=(temp != null && temp.length()>0) ? Byte.valueOf(temp) : defaultValue;
	    	} catch (Exception e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	    	} 
	    	return defaultValue;
	    }
	
}
